from django.db.models import Model, CharField, ForeignKey, \
                             IntegerField, ManyToManyField, BooleanField

class Serializable(object):
    def __on__(self):
        """Returns the object notation of the current object
        """
        raise NotImplementedError
                                 

class Country(Model, Serializable):
    code = CharField(max_length=3, primary_key=True)
    name = CharField(max_length=32)
        
    def __str__(self):
        return self.name
    
    def __repr__(self):
        return "<Country: '%s'>" % str(self)
        
    def __on__(self):
        return {"code":self.code,
                "name":self.name}
        
class League(Model):
    name = CharField(max_length=32)
    country = ForeignKey(Country)
    level = IntegerField()
    numOfTeams = IntegerField()
    numPromoted = IntegerField()
    numRelegated = IntegerField()

    def __str__(self):
        return "%s %s" % (self.name, str(self.country))
    def __repr__(self):
        return "<League '%s'>" % (str(self))
    def __on__(self):
        return {'id':self.id,
                'name':self.name,
                'country':self.country.code,
                'level':self.level,
                'numOfTeams':self.numOfTeams,
                'numPromoted':self.numPromoted,
                'numRelegated':self.numRelegated}
    

class Team(Model):
    name = CharField(max_length=32)
    country = ForeignKey(Country)
    lastLeague = ForeignKey(League)
    
    def __str__(self):
        return self.name
    
    def __repr__(self):
        return "<Team '%s'>" % (str(self))
    
    def __on__(self):
        return {'id': self.id,
                'name': self.name,
                'country': self.country.code,
                'lastLeague': self.lastLeague.id}

class Season(Model):
    year = CharField(max_length=10)
    league = ForeignKey(League)
    teams = ManyToManyField(Team)
    
    def __str__(self):
        return "%s %s" % (self.year, str(self.league))
    
    def __repr__(self):
        return "<Season '%s'>" % (str(self))
        
        
class Round(Model):
    roundNumber = IntegerField()
    season = ForeignKey(Season)

    def __str__(self):
        return "Round %s of %s" % (self.roundNumber, str(self.season))
    
    def __repr__(self):
        return "<Round '%s'>" % (str(self))
    
    class Meta:
        ordering = ('id', 'roundNumber',)
        
   
class Match(Model):
    round = ForeignKey(Round)
    team1 = ForeignKey(Team, related_name='team1')
    team1Score = IntegerField(null=True)
    team2 = ForeignKey(Team, related_name='team2')
    team2Score = IntegerField(null=True)
    isPending = BooleanField(default=True)
    
    def mirror(self):
        retval = Match(team1=self.team2, team2=self.team1)
        return retval
        
    def isTie(self):
        return self.winner == None and self.loser == None
    
    def winner(self):
        if self.isPending: return None
        if self.team1Score > self.team2Score: return self.team1
        elif self.team2Score > self.team1Score: return self.team2
        else: return None
    
    def loser(self):
        if self.isPending: return None
        if self.team1Score > self.team2Score: return self.team2
        elif self.team2Score > self.team1Score: return self.team1
        else: return None
        
    def __str__(self):
        if self.isPending:
            return "%s vs %s" % (self.team1, self.team2)
        else:
            return "%s %s:%s %s" % (self.team1, self.team1Score,
                                    self.team2_score, self.team2)
    class Meta:
        ordering = ('round', )
        
    